package demo;

import com.google.gson.*;

import java.io.IOException;
import java.util.logging.Logger;
import java.util.*;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GetOutsideGeofence extends HttpServlet {
	private static final Logger log = Logger.getLogger(MyTrackServlet.class.getName());	
	EntityManager em;

	private String imei;
    private String deleteAlert;
    private String OutsideGeofenceData;
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		
		Gson gson = new Gson();
		String json = null;
		//String imei = "354037050138621";

		
		
		//changes 3
		imei = req.getSession().getAttribute("imei").toString();
		
			//changes 7
			List<OutsideGeofence> mList = getOutsideGeofenceData(imei);
			//List<MapData> mList = ao.getData(max);
			json = gson.toJson(mList);
		
		resp.setContentType("text/plain");
		resp.getWriter().println("{\"aaData\":" + json +"}");	
		
		
	}

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		
		doGet(req,resp);
	}

	public List<OutsideGeofence> getOutsideGeofenceData(String imei) {
		List<OutsideGeofence> list = new ArrayList<OutsideGeofence>();
		em = EMF.get().createEntityManager();
		Query q = em.createQuery("select d from OutsideGeofence d where imei='"
				+ imei + "'");
		list = q.getResultList();
		em.close();

		return list;
	}
}